import { Component, OnInit, Input, EventEmitter, Output, Host, Inject, forwardRef, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { Pagination } from '../../../public/serve/pcrudInfo';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
interface Response {
  result: string[];
  result_code: string;
  message: string;
  length: number;
}

@Component({
  selector: 'app-role-roleby',
  templateUrl: './role-roleby.component.html',
})
export class RoleRolebyComponent implements OnInit {

  public roleIdto: string;

  constructor(
    private http: HttpClient, 
    public server: Service, 
    private router: Router, 
    private tableListService: CudTableService, 
    public removeTableService: RemoveTableService,
    private hintsService :HintsService 
  ) {

  }

  ngOnInit() {
    //接受角色Id
    this.rolebyeventEmit = this.server.rolebyeventEmit.subscribe((value: any) => {
      this.roleIdto = value;
    });

  }
  //取消订阅
  public rolebyeventEmit: any;
  ngOnDestroy() {
    if (this.rolebyeventEmit) {
      this.rolebyeventEmit.unsubscribe();
    }
  }

  public isclick: any = true;

  public userselectedItems = [];
  public userselectedItemsName = [];
  //选中数据
  outselectedItemsclick(val) {
    //console.log(val)
    this.userselectedItems = val;
  }
  outselectedItemsNameclick(val) {
    //console.log(val)
    this.userselectedItemsName = val;
  }
  //双击数据
  outdoubleIdName(val) {
    this.userselectedItems = [val.id];
    this.userselectedItemsName = [val.name];
    this.bindUser();
  }
  outdoubleId(val) {
    this.removeTableService.getRemoveTableId = [val];
    this.unbound();
  }
  //bind user
  public bindUser(): void {
    let body = {
      userId: this.userselectedItems,
      userName: this.userselectedItemsName,
      roleId: this.roleIdto
    };
    //console.log(body)
    if (this.userselectedItems.length == 0 && this.userselectedItemsName.length == 0) {
      this.hintsService.messages("请选择要关联的用户！","9999");
    } else {
      this.http.post<Response>(this.server.baseUrl+'admin/roles/addUserRole', body)
        .subscribe(res => {
          //console.log(res.result)
          this.hintsService.messages(res.message,res.result_code);
          if (res.result_code == '0000') {
            this.server.prevent = false;
            this.server.rolebylefteventEmit.emit(true);
          } else {
            this.server.prevent = false;
            // this.userselectedItems = [];
            // this.userselectedItemsName = [];
          }
        });
    }
  }


  public roleselectedItems = [];
  //解除绑定
  public unbound(): void {
    let body = {
      userRoleId: this.removeTableService.getRemoveTableId,
    };
    //console.log(body)
    if (this.removeTableService.getRemoveTableId.length == 0) {
      this.server.prompt = { prompt: true, success: false, fail: false, promptStatement: "请选择已关联的用户！" }
      this.server.promptclose;
    } else {
      this.http.post<Response>(this.server.baseUrl+'admin/roles/deleteUserRoles', body)
        .subscribe(res => {
          this.hintsService.messages(res.message,res.result_code);
          if (res.result_code == '0000') {
            this.server.rolebylefteventEmit.emit(true);
            //this.roleselectedItems=[];
          }
        });
    }
  }


}
